package fr.syntheses.blog.Model.DAO.SQL;

import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.ServletContext;
import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;




// Classe de connexion
public class ModeleDAO  implements DAO
{	
	DataSource dataSource=null;
	Connection connection=null;
	
	Logger log = Logger.getLogger(ModeleDAO.class);
	
	// R�cup�rer une connexion
	public Connection getConnection() 
	{
		

		ServletContext servletContext = ServletActionContext.getServletContext();
		
		if(this.dataSource==null)
		{
			dataSource=(DataSource)servletContext.getAttribute("dataSource");
			
			
		}
		
		if(dataSource!=null)
		{

			try
			{
				connection = dataSource.getConnection();
				
				
			}
			catch(SQLException e)
			{
				System.out.println(e);
			}
		}
		
		// retourner la connexion
		return connection;
	}
	
	
	// Positionner une dataSource
	public void setConnection(DataSource dataSource)
	{
		this.dataSource=dataSource;
	}
}
